#include "Targeter.h"




void Targeter::init(Camera* pCam)
{
	cam = pCam;
	ts_a = .6;
	trg_s = 3;
	old_x = old_y = 0;
}

/*----------------------------------------------------------------------------*/
void Targeter::mouseMove(int x, int y)
{
	cam->rotateWithMouseTargeter(x,y, old_x, old_y);
	old_x = x;
	old_y = y;
}

/*----------------------------------------------------------------------------*/
void Targeter::mouseDrag(int x, int y)
{
	if (t_but == GLUT_RIGHT_BUTTON)
		trg_s += (float).05 * (old_y - y);

	if (t_but == GLUT_LEFT_BUTTON)
	{
		glMatrixMode (GL_MODELVIEW);
		glLoadIdentity ();

		cam->update();

		GLint	viewport[4];
		GLdouble projMat[16];
		GLdouble modlMat[16];

		glGetDoublev (GL_PROJECTION_MATRIX, projMat);
		glGetDoublev (GL_MODELVIEW_MATRIX, modlMat);
		glGetIntegerv(GL_VIEWPORT, viewport);

		gluUnProject(x, viewport[3]-y, 1,
			modlMat,
			projMat,
			viewport,
			&tpx, &tpy, &tpz);           

	}
	old_x = x;
	old_y = y;
}

/*----------------------------------------------------------------------------*/
Vec3f Targeter::mouseClick(int button, int state, int x, int y) 
{
	t_but=button;
	old_y = y;
	old_x = x;

	if (button == GLUT_LEFT_BUTTON && state == 1)
	{
		glMatrixMode (GL_MODELVIEW);
		glLoadIdentity ();

		cam->update();


		GLint	viewport[4];
		GLdouble projMat[16];
		GLdouble modlMat[16];
		float z;

		glGetDoublev (GL_PROJECTION_MATRIX, projMat);
		glGetDoublev (GL_MODELVIEW_MATRIX, modlMat);
		glGetIntegerv(GL_VIEWPORT, viewport);

		glReadPixels( x, viewport[3]-y, 1, 1,
			GL_DEPTH_COMPONENT, GL_FLOAT, &z );

		gluUnProject(x, viewport[3]-y, z,               
			modlMat,
			projMat,
			viewport,
			&tpx, &tpy, &tpz);
	}

	return Vec3f(tpx, tpy, tpz);

}

void Targeter::drawTargetSys(Vec3f shooter)
{
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);
	//change alpha value for targeting sphere 
	/*if (ts_a >= .6)
		ts_a += -.005f;
	else if (ts_a <=.12)
		ts_a += .005f;*/

	Vec3f temp = cam->getTargetPos();
	/*cam->setTargetPos(temp);
	cam->update();*/

	//glRotatef(t_roty, 0, 1, 0);
	//glRotatef(t_rotx, 1, 0, 0);
	//glPushMatrix();

	GLUquadric *q1;

	//draw target fire point
	glPushMatrix();
	glColor4f(200, 00, 00, ts_a);

	glTranslatef(tpx, tpy, tpz);
	q1 = gluNewQuadric();
	gluQuadricOrientation (q1, GLU_OUTSIDE);
	gluQuadricDrawStyle(q1, GLU_FILL);
	gluSphere(q1, .2, 8, 8);
	glPopMatrix();

	//draw targeting fire line
	glColor4f(200, 00, 00, ts_a);
	glPushMatrix();
	glBegin(GL_LINES);
	glVertex3f(shooter.x, shooter.y, shooter.z);
	glVertex3f(tpx, tpy, tpz);
	glEnd();
	glPopMatrix();


	//draw targeting sphere
	glPushMatrix();
	glColor4f(00, 200, 200, ts_a);

	//glTranslatef(0,0,0);
	glTranslatef(temp.x, temp.y, temp.z);
	q1 = gluNewQuadric();
	gluQuadricOrientation (q1, GLU_OUTSIDE);
	gluQuadricDrawStyle(q1, GLU_LINE);
	gluSphere(q1, trg_s, 1.5*trg_s, 1.5*trg_s);
	glPopMatrix();

	glPushMatrix();
	glColor4f(00, 200, 200, 0);

	glTranslatef(temp.x, temp.y, temp.z);
	q1 = gluNewQuadric();
	gluQuadricOrientation (q1, GLU_OUTSIDE);
	gluQuadricDrawStyle(q1, GLU_FILL);
	gluSphere(q1, trg_s, 1.5*trg_s, 1.5*trg_s);
	glPopMatrix();
	
	

	//glPopMatrix();
	glEnable(GL_LIGHTING);
	glEnable(GL_TEXTURE_2D);
}